package com.jyw.business.de.domain.vo.web;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.jyw.business.de.constant.DemandProcessConstant;
import com.jyw.business.de.domain.DeDemandProcess;
import com.jyw.common.annotation.Conversion;
import com.jyw.common.annotation.Excel;
import com.jyw.common.constant.SysDictTypeConstant;
import com.jyw.common.enums.ConversionType;
import com.jyw.common.utils.DictUtils;
import com.jyw.common.utils.bean.BeanUtils;
import com.jyw.system.utils.UserUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldNameConstants;
import org.springframework.util.ObjectUtils;

import java.util.Date;

@ApiModel("需求流程")
@Data
@FieldNameConstants
@NoArgsConstructor
public class DemandProcessVO {

    @ApiModelProperty("操作记录id")
    private Long id;

    /** 需求id */
    @ApiModelProperty("需求id")
    private Long demandId;

    /** 操作类型（数据字典） */
    @ApiModelProperty("操作类型（数据字典）")
    @Conversion(value = ConversionType.DICT,dictType = SysDictTypeConstant.DE_DEMAND_PROCESS_STATE)
    private String operState;

    @ApiModelProperty("操作类型标签")
    private String operStateToName;

    /** 参数 */
    @ApiModelProperty("参数")
    private String processValue;

    @ApiModelProperty("参数对象")
    private Object processObj;


    /** 创建人(创建人) */
    @ApiModelProperty("操作人id(创建人)")
    @Conversion(value = ConversionType.USER,name = Fields.createUserName)
    private Long createUser;

    /** 创建人(创建人) */
    @ApiModelProperty("操作人名称(创建人)")
    private String createUserName;

    /** 创建时间(创建时间) */
    @ApiModelProperty("创建时间(创建时间)")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    public DemandProcessVO(DeDemandProcess data) {
        BeanUtils.copyProperties(data,this);
        switch (data.getOperState()) {
            case DemandProcessConstant.DEMAND:
                JSONObject json = JSONObject.parseObject(data.getProcessValue());
                json.put("demandSourceToName", DictUtils.getDictLabel(SysDictTypeConstant.DE_DEMAND_SOURCE,json.getString("demandSource")));
                setProcessObj(json);
                break;
            case DemandProcessConstant.ALLOCATION:
                setProcessObj(UserUtils.userIdToName(data.getProcessValue()));
                break;
            case DemandProcessConstant.TRANSFER:
                json = JSONObject.parseObject(data.getProcessValue());
                json.put("userName", UserUtils.userIdToName(json.getString("user")));
                setProcessObj(json);
                break;
            case DemandProcessConstant.PAUSE:
            case DemandProcessConstant.TERMINATION:
                json = JSONObject.parseObject(data.getProcessValue());
                setProcessObj(json);
                break;
            case DemandProcessConstant.CONVERT_ORDER:
                //todo 待处理
                break;
            case DemandProcessConstant.PROJECT_AUDIT:
                //todo 待处理
                break;
        }
    }
}
